﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entities.Entities;
using Utilities.Log;
using Utilities.Expand;
using Newtonsoft.Json;
using MongoDB.Driver;
using MongoDB.Bson;
using System.Web.Security;
using Utilities;
namespace Logic.Members
{
    public class Member : LogicBase, IMember
    {
        private Entities.Interface.IMember _member = new Entities.Operate.Member();
        private Utilities.Log.LogHelper log = new Utilities.Log.LogHelper(typeof(Member));

        public Member(System.Web.HttpContextBase args)
        {
            this.context = args;
        }

        /// <summary>
        /// 注册新用户
        /// </summary>
        /// <param name="username">用户名</param>
        /// <param name="pwd">密码</param>
        public Model.MethodResult Register(string phone, string password)
        {
            Entities.Entities.Member user = new Entities.Entities.Member();
            QueryDocument query = new QueryDocument();
            BsonDocument param = new BsonDocument();
            param.Add("UserName", phone);
            param.Add("Phone", phone);
            query.Add(param);
            if (_member.FindAs(query).Count() <= 0)
            {
                try
                {
                    user.UserName = phone;
                    user.Phone = phone;
                    user.Password = password.ToMD5();
                    _member.Insert(user);
                    LogicBase.EventNotify("MemberRegister").Notify(context, null);
                    
                    return new Model.MethodResult()
                    {
                        status = true,
                        code = 0,
                        message = JsonConvert.SerializeObject(user)
                    };
                }
                catch (Exception e)
                {
                    log.error(e);
                    LogicBase.EventNotify("MemberRegister").Notify(context, null);
                    return new Model.MethodResult()
                    {
                        status = false,
                        code = 10001,
                        message = "注册失败",
                    };
                }
            }
            else
            {
                LogicBase.EventNotify("MemberRegister").Notify(context, null);
                return new Model.MethodResult()
                {
                    status = false,
                    code = 10002,
                    message = "此帐号已经注册",
                };
            }
        }

        public bool ReSetPassWord(string uid, string newpassword)
        {
            throw new NotImplementedException();
        }

        public bool FindPassWord(string validatecode, string newpassword)
        {
            throw new NotImplementedException();
        }

        public Entities.Entities.Member GetMemberById(string id)
        {
            throw new NotImplementedException();
        }

        public Entities.Entities.Member GetMemberByPhone(string Phone)
        {
            throw new NotImplementedException();
        }
    }
}
